<?php
class Default_Form_ValidateSupports{
	//Chứa những thông báo lỗi của form
	protected $_messagesError;
	
	//Mảng chứa dữ liệu sau khi kiểm tra
	protected $_arrData;
	
	public function __construct($arrParam = array(),$options = null){
		
		//Kiểm tra name
		$validator 	= new Zend_Validate();
		if($arrParam['action'] == 'add'){
			$options = array('table'=>'supports','field'=>'name');
		}else if($arrParam['action'] == 'edit'){
			$clause = ' id !=' . $arrParam['id'];
			$options = array('table'=>'supports','field'=>'name','exclude'=>$clause);
		}
		$validator	->addValidator(new Zend_Validate_NotEmpty(),true)
					->addValidator(new Zend_Validate_StringLength(3,32),true)
					->addValidator(new Zend_Validate_Db_NoRecordExists($options),true);
		if(!$validator->isValid($arrParam['name'])){
			$messages = $validator->getMessages();
			$this->_messagesError['name'] = 'Name : ' . current($messages);
			$arrParam['name'] = '';
		}
		//Kiểm tra Nick
		$validator 	= new Zend_Validate();
		if($arrParam['action'] == 'add'){
			$options = array('table'=>'supports','field'=>'nick');
		}else if($arrParam['action'] == 'edit'){
			$clause = ' id !=' . $arrParam['id'];
			$options = array('table'=>'supports','field'=>'nick','exclude'=>$clause);
		}
		$validator	->addValidator(new Zend_Validate_NotEmpty(),true)
					->addValidator(new Zend_Validate_StringLength(3,32),true)
					->addValidator(new Zend_Validate_Db_NoRecordExists($options),true);
		if(!$validator->isValid($arrParam['nick'])){
			$messages = $validator->getMessages();
			$this->_messagesError['nick'] = 'Nick : ' . current($messages);
			$arrParam['nick'] = '';
		}
		
		//Kiểm tra type
		if(empty($arrParam['type']) || !isset($arrParam['type'])){
			$arrParam['type'] = 0;
		}
		
		//Kiểm tra status
		if(empty($arrParam['status']) || !isset($arrParam['status'])){
			$arrParam['status'] = 0;
		}
		
		$this->_arrData = $arrParam;
	}
	
	public function isError(){
		//kiểm tra nếu có lỗi trả về giá trị true
		if(count($this->_messagesError) > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public function getMessageError($options = null){
		return $this->_messagesError;
	}
	public function getData($options = null){
		return $this->_arrData;
	}
}